package service

import (
	"blizzard/app/dao"
	"blizzard/app/model"
	"blizzard/app/utils/code"
	"blizzard/app/utils/tool"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var App = new(serviceApp)

type serviceApp struct {}

func (s *serviceApp) Add(r *ghttp.Request, m *model.AppParam) error {
	one, err := dao.BxSoftware.FindOne("name = ?", m.Name)
	if err != nil  {
		return err
	}
	if one != nil {
		return gerror.New("该应用已存在")
	}
	if err = gconv.Struct(m, &one); err != nil {
		return err
	}
	one.SKey = tool.RndString(30)
	one.Remote = ""
	_, err = dao.BxSoftware.Save(one)
	if err != nil {
		return err
	}
	return nil
}

func (s *serviceApp) Change(id int, m *model.AppParam) error {
	one, err := dao.BxSoftware.FindOne("name = ? AND id = ?", m.Name, id)
	if err != nil {
		return err
	}
	if one == nil {
		return gerror.New(code.SoftwareErrorMsg)
	}
	if err = gconv.Struct(m, &one); err != nil {
		return err
	}
	if err = dao.BxSoftware.UPSoftware(m.Name, one); err != nil {
		return err
	}
	return nil
}

func (s *serviceApp) GetInfo(id int, m *model.AppInfoParam) (interface{}, error) {
	if tool.GetTime() + 100 < m.Time || tool.GetTime() - 100 > m.Time {
		return nil, gerror.New(code.RequestExpired)
	}
	app, err := dao.BxSoftware.FindOne("id = ? AND s_key = ?", id, m.AppKey)
	if err != nil {
		return nil, err
	}
	data := g.Map{}
	data["version"] = app.Version
	data["notice"] = app.Notice
	data["update_data"] = app.UpdateData
	data["update_type"] = app.UpdateType
	data["static_data"] = app.StaticData
	data["trial_interval"] = app.TrialInterval
	data["software_state"] = app.SoftwareState
	data["unbind_type"] = app.BindingdelType
	data["binding_type"] = app.BindingType
	data["give_time"] = app.GiveTime
	return data, nil
}

func (s *serviceApp) GetInfoAll(id int, m *model.AppInfoParam) (interface{}, error) {
	if tool.GetTime() + 100 < m.Time || tool.GetTime() - 100 > m.Time {
		return nil, gerror.New(code.RequestExpired)
	}
	app, err := dao.BxSoftware.
		FindOne("id = ? AND s_key = ?", id, m.AppKey)
	if err != nil {
		return nil, err
	}
	return app, nil
}

func (s *serviceApp) AddBlack(r *ghttp.Request, id int, m *model.AddBlackParam) error {
	if tool.GetTime() + 100 < m.Time || tool.GetTime() - 100 > m.Time {
		return gerror.New(code.RequestExpired)
	}
	data := g.Map{}
	if m.Type != 1 && m.Type != 2 {
		return gerror.New("type字段只能为1或2")
	}
	if m.Type == 2 {
		data["feature"] = r.GetClientIp()
	} else if m.Type == 1 && m.Feature != "" {
		data["feature"] = m.Feature
	} else {
		return gerror.New("feature字段为空")
	}
	one, err := dao.BxBlack.
		Where(
			"feature = ? AND type = ? AND (software_id = 0 OR software_id = ?)",
			data["feature"],
			m.Type,
			id,
	).One()
	if err == nil && one != nil {
		return gerror.New(code.FeatureError)
	}
	data["type"] = m.Type
	data["time"] = tool.GetTime()
	data["menber_id"] = 0
	data["software_id"] = id
	_, err = dao.BxBlack.Save(data)
	if err != nil {
		return err
	}
	return nil
}

func (s *serviceApp) ListAll() ([]*model.BxSoftware, error) {
	all, err := dao.BxSoftware.FindAll("id > ?", 0)
	if err != nil {
		return nil, err
	}
	return all, nil
}